package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class DeadLetterConfig {


    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";


    //创建普通队列
    @Bean
    public Queue queue2() {
        return QueueBuilder.durable(queue_dead_2).build();
    }
    //创建TTL队列
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<>();
        //TTL队列     与发送消息时设置的过期时间相比   以时间小为准
        //arguments.put("x-message-ttl",30*60*1000);
        //配置过期转发的死信交换机
        arguments.put("x-dead-letter-exchange",exchange_dead);
        //配置过期转发的死信RoutingKey
        arguments.put("x-dead-letter-routing-key",routing_dead_2);
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }
    //创建交换机
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }
    //绑定
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1).noargs();
    }
    //绑定
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2).noargs();
    }


}
